/*
 * author      : 吴中勤 
 * date        : 2019/3/21
 * description : TODO
 */

package org.wzq.learn.threadpool;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wzq.learn.AppRuntimeEnv;

class RunnableWrapper implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(RunnableWrapper.class);
    private final Runnable task;
    private final String localString;

    RunnableWrapper(Runnable task, AppRuntimeEnv appRuntimeEnv) {
        this.task = task;
        this.localString = appRuntimeEnv.toString();
    }

    @Override
    public void run() {
        if (logger.isInfoEnabled()) logger.info("RunnableWrapper::run start");
        // 将变量设置到线程的变量中去, 此时已经在 线程 中
        AppRuntimeEnvThread t = (AppRuntimeEnvThread) Thread.currentThread();
        t.setLocal(localString);

        // 原有 task
        try {
            task.run();
            if (logger.isInfoEnabled()) logger.info("RunnableWrapper::run end");
        } catch (Exception ex) {
            if (logger.isInfoEnabled()) logger.info("RunnableWrapper exception");
            // TODO 上报 sentry
        }
    }
}